package com.starblink.search.result.ui.fragment.brand

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.common.view.brand.SearchBrandRecommendModel
import com.starblink.basic.apollo.ext.optional
import com.starblink.rocketreserver.FetchBrandRecommendQuery
import com.starblink.rocketreserver.FetchSimilarBrandRecommendQuery
import com.starblink.rocketreserver.SearchBrandsQuery
import com.starblink.rocketreserver.type.BrandRecommendReq
import com.starblink.rocketreserver.type.BrandSearchReq
import com.starblink.rocketreserver.type.SimilarBrandReq
import com.starblink.search.R
import com.starblink.search.result.repository.model.GoodsRecommendTileModel
import com.starblink.search.result.repository.model.GoodsResultEmptyModel
import com.starblink.search.result.ui.fragment.BaseSearchVM
import kotlinx.coroutines.launch

class BrandResultVM : BaseSearchVM() {

    val contentList = arrayListOf<Any>()
    var resultIds = listOf<String>()

    val emptyModel: GoodsResultEmptyModel by lazyOnNone {
        GoodsResultEmptyModel(
            emptyTxt1 = string(com.starblink.basic.style.R.string.default_empty_text1),
            emptyTxt2 = string(R.string.search_brand_empty_tip)
        )
    }

    val recTitleModel: GoodsRecommendTileModel by lazyOnNone {
        GoodsRecommendTileModel(
            text = string(com.starblink.basic.style.R.string.you_may_like)
        )
    }

    val contentLD = MutableLiveData<Boolean>()


    var contentSearch = ""
    var searchWordSource: Int? = null

    var pageNo = 1

    var recPageNo = 1

    private var recSizeTag = 0

    fun loadData() {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                SearchBrandsQuery(
                    params = BrandSearchReq(
                        contentSearch = contentSearch.optional(),
                        pageNo = pageNo.optional(),
                        pageSize = PAGE_SIZE_1.optional()
                    )
                )
            )
            val list =
                response?.data?.searchBrands?.map { it.brandVOF } ?: arrayListOf()

            resultIds = list.map { it.brandId.toString() }
            if (list.isNullOrEmpty()) {
                if (pageNo == 1) {
                    contentList.add(emptyModel)
                }
                loadRecStore()
            } else {
                loadSuccess()
                contentList.addAll(list)
                contentLD.postValue(list.isNotEmpty())

                loadRecStore()
            }

        }
    }

    fun loadRecStore() {
        viewModelScope.launch {
            val list: List<SearchBrandRecommendModel> =
                if (resultIds.isNotEmpty()) {
                    val response = SkGqlClient.skQuery(
                        FetchSimilarBrandRecommendQuery(
                            params = SimilarBrandReq(
                                pageNo = recPageNo.optional(),
                                pageSize = PAGE_SIZE.optional(),
                                brandIds = Optional.present(resultIds)
                            ).optional()
                        )
                    )


                    response?.data?.fetchSimilarBrandRecommend?.mapIndexed { index, it ->
                        SearchBrandRecommendModel(
                            it.brandVOF,
                            index = index + recSizeTag,
                        )
                    }
                        ?: arrayListOf()
                } else {
                    val response = SkGqlClient.skQuery(
                        FetchBrandRecommendQuery(
                            params = BrandRecommendReq(
                                pageNo = recPageNo.optional(),
                                pageSize = PAGE_SIZE.optional(),
                            )
                        )
                    )

                    response?.data?.fetchBrandRecommend?.mapIndexed { index, it ->
                        SearchBrandRecommendModel(
                            it.brandVOF,
                            index = index + recSizeTag,
                        )
                    }
                        ?: arrayListOf()
                }

            recSizeTag += list.size

            if (recPageNo == 1) {
                contentLD.postValue(list.isNotEmpty())
                if (list.isNotEmpty()) {
                    contentList.add(recTitleModel)
                    contentList.addAll(list)
                    loadSuccess()
                } else {
//                    loadNoData()
                }
            } else {
                contentList.addAll(list)
                contentLD.postValue(list.isNotEmpty())
                loadSuccess()
            }

        }
    }

    val contentHasResult: Boolean
        get() = contentList.firstOrNull() != emptyModel

    companion object {

        const val PAGE_SIZE_1 = 20
        const val PAGE_SIZE = 20

    }


}